//
//  PBChangeCityController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/12/25.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBChangeCityController.h"
#import "PBCityModel.h"

@interface PBChangeCityController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation PBChangeCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}

- (void)configure {
    [self configureView];
    [self configureData];
}

- (void)configureView {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
}
- (void)configureData {
    NSDictionary *param = @{};
    [TMNetwork requestGetWithParam:param url:@"/position/city" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.dataSource = [PBCityModel objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBCityModel *model = self.dataSource[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = model.cityName;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBCityModel *model = self.dataSource[indexPath.row];
    if (self.backBlcok) {
        self.backBlcok(model.cityName, model.cityId);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"切换城市";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
